* Table A7b Balance and Summary Statistics by Insurance Purchase Status
* Sample used to study household outcomes
* Last Updated, Niriksha Shetty 03/31/2016

clear
clear mata
clear matrix
set maxvar 20000
set more off

pwd
cap cd c(`pwd')

estimates clear

use "../dta/reduced-form-master.dta", clear

*Using balanced panel
keep if ex_post_balp==1

* control were not offered WI
drop if vtgroup=="Control"

* someone who ever bought insurance
bys id: egen bought_all = max(bought)

local vars "w_real_cons w_real_food_exp w_real_events_exp w_real_gifts_out w_real_nonfood_exp w_real_durables_exp w_real_income w_real_gifts_in w_real_sav w_real_lent w_real_borrow food_sufficiency_child fladder_std outlook_std control_std "

sort id wave

* use lag because survey data for corresponding year collected before marketing
foreach x of local vars {
bys id : gen lag_`x'=`x'[_n-1]
}

local vars1 "lag_w_real_sav lag_w_real_lent lag_w_real_borrow lag_w_real_food_exp lag_w_real_nonfood_exp lag_w_real_durables_exp lag_w_real_events_exp lag_w_real_income lag_w_real_gifts_out lag_w_real_gifts_in lag_food_sufficiency_child lag_fladder_std lag_outlook_std lag_control_std lag_w_real_cons"

qui estpost summarize `vars' if (mkt_year==2005 & bought_all==1)|(mkt_year==2008 & bought_all == 1 & add_sample_w4==1)
est sto A 
qui estpost summarize `vars1' if bought==1 
est sto B 
qui estpost summarize `vars' if (bought_all==0 & mkt_year==2005)| (mkt_year==2008 & bought_all == 0 & add_sample_w4==1) 
est sto C
qui estpost summarize `vars1' if bought==0 
est sto D 
esttab A B C D using "../output/sum-a7b.csv", cells("mean(fmt(2))" replace sd(par fmt(2))) nolabel noabbrev title("Summary statistics") collabels("Mean") mtitle("Baseline-bought" "all-bought" "baseline-no bought" "all-no bought") nonum replace 


* balance tests
foreach var of local vars {
reg `var' bought_all if (mkt_year == 2005)|(mkt_year==2008 & add_sample_w4==1), cluster(villageno)
est sto A
test bought_all
estadd scalar pval = r(p)
esttab A using "../output/bal-a7b.csv", stats(N pval, fmt(0 2) labels("Obs""P-val"))  nonum nomtitles append
}
estimates clear

foreach var of local vars1 {
reg `var' bought , cluster(villageno)
est sto B
test bought
estadd scalar pval = r(p)
esttab B using "../output/bal-a7b.csv", stats(N pval, fmt(0 2) labels("Obs""P-val"))  nonum nomtitles append
estimates clear
}
